﻿using System;

namespace Sustainalytics.ESG.Entities
{
    public class KeyEsgIssueIndicator
    {
        public KeyEsgIssueIndicatorType Type { get; set; }
        public string Number { get; set; }
        public string Name { get; set; }
        public string Score { get; set; }
        public string Average { get; set; }
        public string BestPractice { get; set; }
        public int MaterialBestPracticeGap { get; set; }
        public bool HidenToClient { get; set; }
        public string AnswerCategory { get; set; }
        public bool IsControversyIndicator { get; set; }
    }

    public enum KeyEsgIssueIndicatorType
    {
        StandardIndicator = 0,
        EventIndicator = 1
    }

    public class KeyEsgIssueIndicatorSorter
    {
        public static int Sort(KeyEsgIssueIndicator i1, KeyEsgIssueIndicator i2)
        {
            if (i1 == null)
            {
                if (i2 == null)
                {
                    // If i1 is null and i2 is null, they're equal. 
                    return 0;
                }
                // If i1 is null and i2 is not null, i2 is greater. 
                return -1;
            }

            if (string.IsNullOrEmpty(i1.Number))
            {
                return -1;
            }

            // If i1 is not null...
            if (i2 == null)
            // ...and i2 is null, i1 is greater.
            {
                return 1;
            }
            if (string.IsNullOrEmpty(i2.Number))
            {
                return 1;
            }


            if (i1.Number == i2.Number)
                return 0;

            var separator = new string[1];
            separator[0] = ".";
            var i1Array = i1.Number.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            var i2Array = i2.Number.Split(separator, StringSplitOptions.RemoveEmptyEntries);

            var i1ArrayLength = i1Array.Length;
            var i2ArrayLength = i2Array.Length;

            var minLength = Math.Min(i1ArrayLength, i2ArrayLength);

            if (i1Array[0] == "E" && i2Array[0] == "S")
            {
                return -1;
            }
            if (i1Array[0] == "S" && i2Array[0] == "E")
            {
                return 1;
            }
            if (i1Array[0] == "S" && i2Array[0] == "G")
            {
                return -1;
            }
            if (i1Array[0] == "G" && i2Array[0] == "S")
            {
                return 1;
            }

            if (i1Array[0] == "E" && i2Array[0] == "G")
            {
                return -1;
            }
            if (i1Array[0] == "G" && i2Array[0] == "E")
            {
                return 1;
            }

            //Foreach theme, controversy indicators must be placed at the bottom
            if (i1.IsControversyIndicator != i2.IsControversyIndicator)
                return i1.IsControversyIndicator.CompareTo(i2.IsControversyIndicator);

            //"E -> S -> G"
            //i1 = E.1.2.7
            //i2 = E.1.2.7.1.2

            for (var i = 1; i < minLength; i++)
            {
                var compareResult = int.Parse(i1Array[i]).CompareTo(int.Parse(i2Array[i]));

                if (compareResult != 0)
                {
                    return compareResult;
                }
            }

            if (i1ArrayLength == i2ArrayLength)
            {
                return 0;
            }

            if (i1ArrayLength > i2ArrayLength)
            {
                return 1;
            }

            return -1;
        }
    }


}
